package com.game.doudizhu.repository;

import com.game.doudizhu.model.Player;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface PlayerRepository extends JpaRepository<Player, Long> {
    
    // 通过用户名查找玩家，返回 Optional
    Optional<Player> findByUsername(String username);
    
    // 检查用户名是否存在
    boolean existsByUsername(String username);
    
    // 通过用户名和密码查找玩家，返回 Optional
    Optional<Player> findByUsernameAndPassword(String username, String password);
    long countByRegisterTimeAfter(LocalDateTime time);

    // 按状态查询用户
    List<Player> findByStatus(String status);

    // 按用户名模糊搜索
    List<Player> findByUsernameLike(String username);

    // 按分数范围查询
    List<Player> findByScoreBetween(Integer minScore, Integer maxScore);

    // 查询指定时间段内注册的用户
    List<Player> findByRegisterTimeBetween(LocalDateTime startTime, LocalDateTime endTime);

    // 统计各状态用户数量
    @Query("SELECT p.status, COUNT(p) FROM Player p GROUP BY p.status")
    List<Object[]> countByStatusGroup();
} 